# 调参案例

from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

cancer=load_breast_cancer()
x_train, x_test, y_train, y_test = train_test_split(cancer.data,cancer.target,test_size=0.3)

rfc=RandomForestClassifier(random_state=50)
score=cross_val_score(rfc, cancer.data, cancer.target, cv=10).mean()
print(score)

# 调参
# 学习曲线是逐个调整参数的好方法
# n_estimators学习曲线
test=[]
for i in range(40):
    rfc = RandomForestClassifier(criterion='entropy'
                                 , random_state=50
                                 , n_estimators=i+1
                                 # , max_depth=5
                                 )
    rfc = rfc.fit(x_train, y_train)
    score = cross_val_score(rfc, cancer.data, cancer.target, cv=10).mean()
    test.append(score)
# print(test)
print(max(test),test.index(max(test))+1)
plt.plot(range(1, 41), test, color="red", label="n_estimators")
plt.legend()
plt.show()

""""
网格搜索可以一次找到最优结果，并且返回最优结果的参数
通过网格搜索逐一找到参数的最优值，从粗到细
"""
# para_grid={'n_estimators':np.arange(10,100,10)}
# para_grid={'max_depth':np.arange(7,13,1)}
para_grid={'max_features':np.arange(5,20,1)}
rfc = RandomForestClassifier(criterion='entropy'
                                 , random_state=50
                                 , n_estimators=38
                                 , max_depth=10
                                 )
gs=GridSearchCV(rfc,para_grid,cv=10)
gs.fit(cancer.data,cancer.target)
print(gs.best_params_)
print(gs.best_score_)
